package com.fqdf.framework.android.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.fqdf.framework.android.util.DialogUtil;

public abstract class BaseFragment extends Fragment {

    private MaterialDialog submitNoCancelprogressDialog;
    private MaterialDialog progressDialog;
    private MaterialDialog noCancelprogressDialog;
    private MaterialDialog createNoCancelUploadImgDialog;

    protected boolean isVisible;

    // 标志位，标志已经初始化完成。
    private boolean isPrepared;

    private MaterialDialog curNoInterruptDialog;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    private void lazyLoad() {
        if (!isPrepared || !isVisible) {
            return;
        }
        initDataIntoView();
    }

    /**
     * 在这里实现Fragment数据的缓加载.
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    private void onVisible() {
        lazyLoad();
    }

    private void onInvisible() {
    }

    /**
     * 当界面可见的时候，会回调到这个方法，用来加载数据。
     */
    protected void initDataIntoView() {
    }


    /**
     * 获取提交等待框-不可取消
     *
     * @return
     */
    protected MaterialDialog getNoCancelSubmitDialog() {
        if (submitNoCancelprogressDialog == null) {
            submitNoCancelprogressDialog = DialogUtil.createNoCancelSubmit(getContext());
        }
        return submitNoCancelprogressDialog;
    }

    /**
     * 获取加载等待框
     *
     * @return
     */
    protected MaterialDialog getProgressDialog() {
        if (progressDialog == null) {
            progressDialog = DialogUtil.createLoadingDialog(getContext());
        }
        return progressDialog;
    }

    /**
     * 获取加载等待框--不可取消
     *
     * @return
     */
    protected MaterialDialog getNoCancelProgressDialog() {
        if (noCancelprogressDialog == null) {
            noCancelprogressDialog = DialogUtil.createNoCancelLoadingDialog(getContext());
        }
        return noCancelprogressDialog;
    }

    /**
     * 创建上传文件等待的dialog
     *
     * @return
     */
    protected MaterialDialog getUploadImgProgressDialog() {
        if (createNoCancelUploadImgDialog == null) {
            createNoCancelUploadImgDialog = DialogUtil.createNoCancelUploadImgDialog(getContext());
        }
        return createNoCancelUploadImgDialog;
    }

    /**
     * 隐藏dialog
     *
     * @param dialog
     */
    protected void hidenMaterialDialog(MaterialDialog dialog) {
        DialogUtil.hideIndeterminateProgress(dialog);

    }
}
